<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <title>欢迎页面-X-admin2.0</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
  <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
  <link rel="stylesheet" href="/css/font.css">
  <link rel="stylesheet" href="/css/xadmin.css">
  <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
  <script type="text/javascript" src="/lib/layui/layui.js" charset="utf-8"></script>
  <script type="text/javascript" src="/js/xadmin.js"></script>
  <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
  <!--[if lt IE 9]>
  <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
  <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
  <![endif]-->
</head>

<body>
<div class="x-nav">
      <span class="layui-breadcrumb">
        <a href="">首页</a>
        <a href="">管理员管理</a>
        <a>
          <cite>权限管理</cite></a>
      </span>
  <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
    <i class="layui-icon" style="line-height:30px">ဂ</i></a>
</div>
<div class="x-body">
    <span class="x-right" style="line-height:40px">共有数据：<span id="total"></span>条</span>
  <table class="layui-table">
    <thead>
    <tr>
      <th>ID</th>
      <th>用户ID</th>
      <th>用户名</th>
      <th>用户角色</th>
      <th>管理角色</th>
    </thead>
    <tbody id="data-body">
    <tr>

      <td>1</td>
      <td>admin/user/userlist</td>
      <td>会员列表</td>
      <td>会员相关</td>
      <td class="td-manage">
        <a title="编辑"  onclick="x_admin_show('编辑','xxx.html')" href="javascript:;">
          <i class="layui-icon">&#xe642;</i>
        </a>
        <a title="删除" onclick="member_del(this,'要删除的id')" href="javascript:;">
          <i class="layui-icon">&#xe640;</i>
        </a>
      </td>
    </tr>
    </tbody>
  </table>
  <div class="page" id="pages">
    <div>
      <a class="prev" href="">&lt;&lt;</a>
      <a class="num" href="">1</a>
      <span class="current">2</span>
      <a class="num" href="">3</a>
      <a class="num" href="">489</a>
      <a class="next" href="">&gt;&gt;</a>
    </div>
  </div>

</div>




<script>

  layui.use('laydate', function(){
    var laydate = layui.laydate;

    //执行一个laydate实例
    laydate.render({
      elem: '#start' //指定元素
    });

    //执行一个laydate实例
    laydate.render({
      elem: '#end' //指定元素
    });
  });


  //分页与搜索

  let nowPage = 1;
  let pageSize = 5;

  /*初始化页码*/
  function initPages(data) {
    // 容器
    let div = $("<div></div>");
    // 上一页
    let pre = $('<a class="prev" pre-page="'+data.prePage+'" has-prev-page="'+data.hasPreviousPage+'" href="javascript:prePage();">&lt;&lt;</a>');
    div.append(pre);
    // 动态的渲染页数
    $(data.navigatepageNums).each(function () {
      let pageA;
      if(data.pageNum == this){
        pageA = $('<span class="current">'+this+'</span>');
      }else{
        pageA = $('<a class="num" href="javascript:changePage('+this+');">'+this+'</a>');
      }
      div.append(pageA);
    });
    // 下一页
    let next = $('<a class="next" next-page="'+data.nextPage+'" has-next-page="'+data.hasNextPage+'" href="javascript:nextPage();">&gt;&gt;</a>');
    div.append(next);

    // 加入外层容器
    $(".page").empty();
    $(".page").append(div);
  }

  /*上一页*/
  function prePage(){
    // 获取上一页
    let prevPageNum = $(".prev").attr("pre-page");
    // 是否有上一页
    let hasPrevPage = $(".prev").attr("has-prev-page");
    // 调用获取数据的方法
    if(hasPrevPage === "true"){
      getPageData(prevPageNum, pageSize);
    }else{
      layer.msg("已经是第一页啦!");
    }
  }

  /*中间的页码跳转*/
  function changePage(page) {
    getPageData(page, pageSize);
  }

  /*下一页*/
  function nextPage() {
    // 获取下一页
    let nextPageNum = $(".next").attr("next-page");
    // 是否有下一页
    let hasNextPage = $(".next").attr("has-next-page");
    // 调用获取数据的方法
    if(hasNextPage === "true"){
      getPageData(nextPageNum, pageSize);
    }else{
      layer.msg("已经是最后一页啦!");
    }
  }

  /*封装查询条件*/
  function getCondition() {
    let data = {};

    return data;
  }

  /*查询数据的方法*/
  function getPageData(page, pageSize){
    let condition = {};
    condition.page = page;
    condition.pageSize = pageSize;
    console.log("page ",page);
    $.ajax({
      type:"post",
      url:"/access/user-role/list",
      data:condition,
      dataType:"json",
      success:function (data) {
        console.log("success");
        console.log(data);
        if(data.code === 200){
          //维护当前页的全局变量nowPage
          nowPage = data.data.pageNum;
          //总条数
          $("#total").text(data.data.total);
          //清空数据
          $("#data-body").empty();
          //遍历数据的数组，生成对应的行
          let id = 1
          $(data.data.list).each(function () {
            //创建数据行的dom对象
            let tr = $('<tr>\n' +
                    ' \n' +
                    '            <td>'+id+'</td>\n' +
                    '            <td>'+this.adminId+'</td>\n' +
                    '            <td>'+this.adminName+'</td>\n' +
                    '            <td>'+this.roleCodes+'</td>\n' +
                    '              <td class="td-manage">\n' +
                    '             <a title="编辑"  onclick="x_admin_show(\'请选择要更改的角色\',\'/access/edit?userName='+this.adminName+'&userId='+this.adminId+'\',\'300\',\'250\')" href="javascript:;">\n' +
                    '                <i class="layui-icon">&#xe642;</i>\n' +
                    '              </a>\n' +
                    '            </td>\n' +
                    '        </tr>')
            //将数据追加到tbody中
            $("#data-body").append(tr);
            id = id+1;
          });
          //初始化页码
          initPages(data.data);
        }

      }
    })
  }

  /*让整个dom加载完成后再执行*/
  $(function () {
    getPageData(nowPage, pageSize);
  })
</script>

</body>

</html>